package com.Action;

import com.Dao.RecruitmentDao;
import com.entity.Page;
import com.entity.Recruitment;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 跳转至未登录的公告界面的动作
 */
public class ToGongGaoAction extends ActionSupport{
    @Override
    public String execute() throws Exception {
        HttpServletRequest request= ServletActionContext.getRequest();
        request.setCharacterEncoding("utf-8");
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        RecruitmentDao rd=ac.getBean(RecruitmentDao.class);
        //查找当前一共有多少条公告
        long totalNumber=rd.searchTotal();
        //设置分页相关信息
        Page page=new Page();
        String number=request.getParameter("pagenumber");
        Pattern pattern = Pattern.compile("[0-9]{1,9}");

        if(number == null) {
            page.setCurrentPage(1);
        } else if(!pattern.matcher(number).matches()){
            //输入要跳转的页数有问题,不是数字,直接返回
            return super.execute();
        } else if(Integer.valueOf(number)>((Page)request.getSession().getAttribute("pages")).getTotalPage()){
            //输入要跳转的页数比总页数大
            return super.execute();
        }else
        {
            page.setCurrentPage(Integer.valueOf(number));
        }
        //先设置一共有多少公告
        page.setTotalNumber(totalNumber);
        //然后设置每页显示几条
        page.setPageNumber(10);
        //计算一共会有几页
        page.count();
        //将分页相关信息存入会话中
        request.getSession().setAttribute("pages",page);
        //根据分页信息查找当前页面的内容并存入会话
//        System.out.println(number);
//        System.out.println(page.getCurrentPage());
        List list=rd.searchGonggao(page.getCurrentPage(),page.getPageNumber());
//        Iterator it=list.iterator();
//        while(it.hasNext()){
//            Recruitment r=(Recruitment)it.next();
//            System.out.println(r.getContent());
//        }
        request.getSession().setAttribute("messageList",list);
        return super.execute();
    }
}
